//
//  WelcomeViewController.swift
//  sinaweibo
//
//  Created by icefs2 on 15/11/13.
//  Copyright © 2015年 icefs2. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage
class WelcomeViewController: UIViewController {

    override func loadView() {
        view = backImage
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
        startAnimation()
        }
    
    private func startAnimation() {
        
        self.iconview.snp_updateConstraints { (make) -> Void in
             make.bottom.equalTo(self.view.snp_bottom).offset(-self.view.bounds.height + 20)
            UIView.animateWithDuration(1.2, delay:0, usingSpringWithDamping: 0.9, initialSpringVelocity: 9.8, options: [], animations: { () -> Void in
                //闭包内部 强制刷新页面
                //将提前收集到的约束的变化 进行更新
                self.view.layoutIfNeeded()
                }) { (_) -> Void in
                UIView.animateWithDuration(0.6, animations: { () -> Void in
                        self.welcomelable.alpha = 1
                        }, completion: { (_) -> Void in
                            print("OK")
                            NSNotificationCenter.defaultCenter().postNotificationName(AppSwitchRootViewControler, object: nil)
                            })
            }
        }
    }
    
    
   private func setupUI() {
    //添加子空间
        view.addSubview(iconview)
        view.addSubview(welcomelable)
    //设置布局
    iconview.snp_makeConstraints { (make) -> Void in
        make.centerX.equalTo(view.snp_centerX)
        make.bottom.equalTo(view.snp_bottom).offset(-188)
        make.height.equalTo(90)
        make.width.equalTo(90)
    }
  
    welcomelable.snp_makeConstraints { (make) -> Void in
        make.centerX.equalTo(iconview.snp_centerX)
        make.top.equalTo(iconview.snp_bottom).offset(20)
        
    }
    //设置图片
    
    iconview.sd_setImageWithURL(UserAccoutviewModel().headURL, placeholderImage: UIImage(named: "avatar_default_big"))
    //设置圆角
    iconview.layer.cornerRadius = 45
    iconview.layer.masksToBounds = true
    }
    
    
    
    private lazy var backImage: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    private lazy var iconview: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    private lazy var welcomelable: UILabel = {
        let l = UILabel()
        l.textColor = UIColor.lightGrayColor()
        l.font = UIFont.systemFontOfSize(19)
        l.textAlignment = NSTextAlignment.Center
        l.text = (UserAccoutviewModel().userName ?? "") + "  欢迎归来"
        l.sizeToFit()
       return l
    }()

}
